﻿using CPPEI.ETL.WebApi.DataContracts.List;
using CPPEI.ETL.WebApi.DataContracts.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace CPPEI.ETL.WebApi.ApiControllers
{
    public class QueryController : ApiController
    {
        [HttpGet]
        public ActionResult<PagedResult<TaskExecLog>> TaskExecLog(string filter, int page)
        {
            int limit = 20;
            PagedList<Domain.Entities.ETaskExecLog> serResult = new Domain.Services.QueryService().QueryTaskExecLog(filter, page * limit, limit);
            if (serResult.IsSuccess == false)
            {
                return new ActionResult<PagedResult<TaskExecLog>>(false, serResult.ErrorCode, serResult.Message, null);
            }

            List<TaskExecLog> list = new List<TaskExecLog>();
            for (int index = 0; index < serResult.Data.Count; index++)
            {
                TaskExecLog item = new TaskExecLog();
                item.Id = serResult.Data[index].Id;
                item.CreatedTime = serResult.Data[index].CreatedTime;
                item.Message = serResult.Data[index].Message;
                item.Mode = serResult.Data[index].Mode;
                item.Result = serResult.Data[index].Result;
                item.TaskId = serResult.Data[index].TaskId;
                item.TaskName = serResult.Data[index].TaskName;
                list.Add(item);
            }
            return new ActionResult<PagedResult<TaskExecLog>>(new PagedResult<TaskExecLog>()
            {
                Page = page,
                Start = serResult.Start,
                Limit = serResult.Limit,
                Total = serResult.Total,
                Count = serResult.Data.Count,
                Items = list
            });
        }
    }
}
